#include "FPCH.h"

#include "FLayoutInterface.h"

namespace Font
{

	InterfaceLayout::InterfaceLayout()
	{

	}

	InterfaceLayout::~InterfaceLayout()
	{

	}


	DecorateLayout::DecorateLayout(InterfaceLayout* a_InterfaceDown)
	{
		m_InterfaceDown = a_InterfaceDown;
	}

	DecorateLayout::~DecorateLayout()
	{

	}

	void DecorateLayout::_OnInstructionNewLine(tb::Vec2& a_Cursor, const TextCommand& a_Data)
	{
		m_InterfaceDown->_OnInstructionNewLine(a_Cursor, a_Data);
	}

	void DecorateLayout::_OnInstructionTab(tb::Vec2& a_Cursor, const TextCommand& a_Data)
	{
		m_InterfaceDown->_OnInstructionTab(a_Cursor, a_Data);
	}

	void DecorateLayout::_OnGlyphKerning(tb::Vec2& a_Cursor, GlyphEntry* a_Entry)
	{
		m_InterfaceDown->_OnGlyphKerning(a_Cursor, a_Entry);
	}

	void DecorateLayout::_OnGlyphTransform(tb::Vec2& a_Cursor, GlyphQuad& a_Quad, GlyphEntry* a_Entry)
	{
		m_InterfaceDown->_OnGlyphTransform(a_Cursor, a_Quad, a_Entry);
	}

	bool DecorateLayout::_OnGlyphAdd(tb::Vec2& a_Cursor, GlyphEntry* a_Entry)
	{
		return m_InterfaceDown->_OnGlyphAdd(a_Cursor, a_Entry);
	}

	void DecorateLayout::SetFont(FaceHandle* a_Font)
	{
		m_InterfaceDown->SetFont(a_Font);
	}


	LayoutBase::LayoutBase()
	{
		m_Font = NULL;
		m_GlyphPrevious = 0;
	}

	LayoutBase::~LayoutBase()
	{

	}

	void LayoutBase::SetFont(FaceHandle* a_Font)
	{
		m_Font = a_Font;
	}

	void LayoutBase::_OnInstructionNewLine(tb::Vec2& a_Cursor, const TextCommand& a_Data)
	{
		a_Cursor.x = 0.f;
		a_Cursor.y += (float)m_Font->GetHeightNewLine();

		m_GlyphPrevious = 0;
	}

	void LayoutBase::_OnInstructionTab(tb::Vec2& a_Cursor, const TextCommand& a_Data)
	{
		m_TabLength = (float)(m_Font->GetGlyph(' ')->GetAdvance().x * m_TabWidth);

		a_Cursor.x += m_TabLength;
	}

	void LayoutBase::_OnGlyphKerning(tb::Vec2& a_Cursor, GlyphEntry* a_Entry)
	{
		wchar_t glyph = a_Entry->glyph->GetCharacter();
		a_Cursor.x += (float)m_Font->GetKerning(m_GlyphPrevious, glyph);
		m_GlyphPrevious = glyph;
	}

	void LayoutBase::_OnGlyphTransform(tb::Vec2& a_Cursor, GlyphQuad& a_Quad, GlyphEntry* a_Entry)
	{
		a_Quad->position_ul.x = a_Entry->pos_start.x;
		a_Quad->position_ul.y = a_Entry->pos_start.y;
		a_Quad->texcoord0_ul.x = a_Entry->tex_start.x;
		a_Quad->texcoord0_ul.y = a_Entry->tex_start.y;

		a_Quad->position_ur.x = a_Entry->pos_end.x;
		a_Quad->position_ur.y = a_Entry->pos_start.y;
		a_Quad->texcoord0_ur.x = a_Entry->tex_end.x;
		a_Quad->texcoord0_ur.y = a_Entry->tex_start.y;

		a_Quad->position_ll.x = a_Entry->pos_start.x;
		a_Quad->position_ll.y = a_Entry->pos_end.y;
		a_Quad->texcoord0_ll.x = a_Entry->tex_start.x;
		a_Quad->texcoord0_ll.y = a_Entry->tex_end.y;

		a_Quad->position_lr.x = a_Entry->pos_end.x;
		a_Quad->position_lr.y = a_Entry->pos_end.y;
		a_Quad->texcoord0_lr.x = a_Entry->tex_end.x;
		a_Quad->texcoord0_lr.y = a_Entry->tex_end.y;
	}

	bool LayoutBase::_OnGlyphAdd(tb::Vec2& a_Cursor, GlyphEntry* a_Entry)
	{

	}

	void LayoutBase::SetTabWidth(unsigned int a_Spaces)
	{
		m_TabWidth = a_Spaces;
	}

}; // namespace Font